﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.SharePoint;
using DVK.SP2007.Constants;
using System.Diagnostics;

namespace DVK.SP2007
{
    /// <summary>
    /// Creates required default eventlog sources and moves resources to web application global resources location.
    /// </summary>
    public class DvkFeatureReceiver : SPFeatureReceiver
    {
        /// <summary>
        /// Occurs after a Feature is activated.
        /// </summary>
        /// <param name="properties">An <see cref="T:Microsoft.SharePoint.SPFeatureReceiverProperties"></see> object that represents the properties of the event.</param>
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            //Create Event Sources for logging application events. Only current Job specific or application wide.
            FeatureReceiverHelper.CreateEventLogSource(Logger.ApplicationLoggingSource);
            FeatureReceiverHelper.CreateEventLogSource(LoggingSources.DvkFeatureReceiver);
            FeatureReceiverHelper.CreateEventLogSource(LoggingSources.GlobalResourcesManager);

            Logger logger = new Logger(LoggingSources.DvkFeatureReceiver, SourceLevels.Information);

            //Must be SPSite, is situated with the feature scope
            var parent = properties.Feature.Parent as SPSite;

            if (parent == null)
            {
                logger.LogError(
                    LoggerEvents.InvalidFeatureParent, 
                    "Invalid parent. Must be SPSite (scope='Site'). Is of type '{1}'.",
                    properties.Feature.Parent.GetType().ToString()
                    );
                return;
            }

            //Copy resx files.
            new GlobalResourcesManager(parent).CopyResourcesUnderWebApp();
        }

        /// <summary>
        /// Occurs when a Feature is deactivated.
        /// </summary>
        /// <param name="properties">An <see cref="T:Microsoft.SharePoint.SPFeatureReceiverProperties"></see> object that represents the properties of the event.</param>
        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
        }

        /// <summary>
        /// Occurs after a Feature is installed.
        /// </summary>
        /// <param name="properties">An <see cref="T:Microsoft.SharePoint.SPFeatureReceiverProperties"></see> object that represents the properties of the event.</param>
        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
        }

        /// <summary>
        /// Occurs when a Feature is uninstalled.
        /// </summary>
        /// <param name="properties">An <see cref="T:Microsoft.SharePoint.SPFeatureReceiverProperties"></see> object that represents the properties of the event.</param>
        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
        }
    }
}
